package leetcode.editor.cn;

public class P1599MaximumProfitOfOperatingACentennialWheel {
    public static void main(String[] args) {
        Solution solution = new P1599MaximumProfitOfOperatingACentennialWheel().new Solution();
        int[] customers = {8,3};
        int board = 5;
        int runningCost = 6;
        System.out.println(solution.minOperationsMaxProfit(customers, board, runningCost));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
            if (boardingCost * 4 <= runningCost) {
                return -1;
            }
            int remain = 0;
            int profit = 0;
            int count = 0;
            int maxCount = 0;
            int maxProfit = 0;
            for (int i = 0; i < customers.length; i++) {
                int customer = customers[i];
                int cost = 0;
                if (remain + customer == 0) {
                    cost = -runningCost;
                } else {
                    if (remain + customer > 4) {
                        remain = remain + customer - 4;
                        cost += boardingCost * 4 - runningCost;
                    } else {
                        cost += (remain + customer) * boardingCost - runningCost;
                        remain = 0;
                    }
                }
                profit += cost;
                if (profit > maxProfit) {
                    maxCount = count + 1;
                    maxProfit = profit;
                }
                count++;
            }
            while (remain > 0) {
                int cost = 0;
                if (remain > 4) {
                    remain = remain  - 4;
                    cost += boardingCost * 4 - runningCost;
                } else {
                    cost += (remain) * boardingCost - runningCost;
                    remain = 0;
                }
                profit += cost;
                if (profit > maxProfit) {
                    maxCount = count + 1;
                    maxProfit = profit;
                }
                count++;
            }
            return maxProfit <= 0 ? -1 : maxCount;
        }

        public int maxSum(int[] arr) {
            int sum = 0, max = arr[0];
            for (int i = 0; i < arr.length; i++) {
                int cur = arr[i];
                if (sum > 0) {
                    sum += cur;
                }else{
                    sum = cur;
                }
                if (sum > max) {
                    max = sum;
                }
            }
            return max;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}